package com.ajk.server.controller.exception;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import com.ajk.server.exception.AjkException;
import com.ajk.server.exception.ErrorConstant;
import com.ajk.server.exception.ErrorInfo;

@ResponseStatus(value = HttpStatus.OK)
@ControllerAdvice(basePackages = "com.ajk.server.controller")
public class BasicExceptionHandler {
    private final static Logger logger = LoggerFactory.getLogger(BasicExceptionHandler.class);

    @ExceptionHandler(IllegalArgumentException.class)
    @ResponseBody
    public ErrorInfo handleIllegalArgument(IllegalArgumentException ex) {
        logger.error(ex.getMessage(), ex);
        return new ErrorInfo(ErrorConstant.PARAM_ERROR.getErrorCode(),ErrorConstant.PARAM_ERROR.getMsg());
    }
    
    
    

    @ExceptionHandler(value = {AjkException.class})
    @ResponseBody
    public ErrorInfo handleServerError(AjkException ex) {
        logger.error(ex.getMessage(), ex);
        String errorMessage = ex.getMessage();
        return new ErrorInfo(ErrorConstant.SERVER_ERROR.getErrorCode(), errorMessage);
    }

    @ExceptionHandler(value = {RuntimeException.class})
    @ResponseBody
    public ErrorInfo handleServerError(RuntimeException ex) {
        logger.error(ex.getMessage(), ex);
        String errorMessage = ex.getMessage();
        return new ErrorInfo(ErrorConstant.SERVER_ERROR.getErrorCode(), errorMessage);
    }
}




